//
//  ZNLMainViewController.swift
//  My_Weibo
//
//  Created by 正能量 on 16/8/12.
//  Copyright © 2016年 正能量. All rights reserved.
//

import UIKit

//var composeBtn: UIButton?

class ZNLMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildController()
        
        dispatch_async(dispatch_get_main_queue(), {
            self.setupTabar()
        })
        
        self.view.tintColor = UIColor.orangeColor()
    
    }
    
    override func viewWillAppear(animated: Bool) {
        view.backgroundColor = UIColor.whiteColor()
        
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        
        
        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)
    }
    
    // MARK: - 监听方法
    // unrecognized selector sent to instance
    // OC 在运行时，向对象发送消息，但是对象没有响应消息，抛出的异常！
    // 一旦使用了 private，函数变成私有，运行循环无法找到此函数，如果发送消息，会抛出异常！
    // @objc 关键字能够在`编译`的时候，让函数能够使用 OC 的消息机制被调用到
    @objc private func clickComposeButton() -> () {
        print("heheh")
    }


    //懒加载btn
    private lazy var composeButton: UIButton = UIButton(title: nil, imageName: "tabbar_compose_icon_add", backgroundImage: "tabbar_compose_button")

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

//MARK: - 设置tabar
extension ZNLMainViewController {
    private func setupTabar() {
        //设置tabbar阴影图片    Nav一样
        tabBar.shadowImage = UIImage.znl_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        
        //UIImage() 不设置图片就是透明
        //设置背景图片
        tabBar.backgroundImage = UIImage(named: "tabBar_background")
        
        
        tabBar.addSubview(composeButton)
        
        composeButton.addTarget(self, action: #selector(ZNLMainViewController.clickComposeButton), forControlEvents: .TouchUpInside)
    }
    

}

// MARK: - 添加控制器
extension ZNLMainViewController {
    
    //添加子控制器
    private func addChildController() {
        
                addChildController(ZNLHomeViewController(), title: "首页", imageName: "tabbar_home")
        
                addChildController(ZNLMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
                addChildViewController(UIViewController())
        
                addChildController(ZNLDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        
                addChildController(ZNLProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    
    //提出的封转的添加自控制器方法
    private func addChildController(vc :UIViewController, title: String, imageName: String) {

        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //设置文本属性
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        
        // 如果要设置文字大小，需要设置 Normal 状态
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .Normal)
        
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }
}

